package com.engine4cloud.tdf.education.controller.web;

import cn.hutool.core.collection.CollUtil;
import com.engine4cloud.tdf.common.core.util.R;
import com.engine4cloud.tdf.common.data.web.response.PageInfo;
import com.engine4cloud.tdf.common.data.web.response.Params;
import com.engine4cloud.tdf.common.log.annotation.SysLog;
import com.engine4cloud.tdf.education.dto.EduCourseResourcePushDTO;
import com.engine4cloud.tdf.education.dto.create.EduCourseCreateDTO;
import com.engine4cloud.tdf.education.dto.query.EduCourseQueryDTO;
import com.engine4cloud.tdf.education.dto.result.EduCourseResultDTO;
import com.engine4cloud.tdf.education.dto.update.EduCourseUpdateDTO;
import com.engine4cloud.tdf.education.service.EduCourseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 课程
 *
 * @author wenjie
 * @date 2024-07-30 14:56:55
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/web/course")
@Tag(description = "eduCourse", name = "课程管理-web")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class EduCourseWebController {

    private final EduCourseService eduCourseService;

    /**
     * 分页查询
     *
     * @param page 分页对象
     * @param dto  课程
     * @return R<PageInfo < EduCourseGroupResultDTO>>
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    public R<PageInfo<EduCourseResultDTO>> pageList(@ParameterObject Params page, @Valid @ParameterObject EduCourseQueryDTO dto) {
        return R.ok(eduCourseService.pageList(page, dto));
    }


    /**
     * 通过id查询课程
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    public R<EduCourseResultDTO> getById(@PathVariable("id") Long id) {
        return R.ok(eduCourseService.getById(id));
    }

    /**
     * 新增课程
     *
     * @param dto 课程
     * @return R
     */
    @Operation(summary = "新增课程", description = "新增课程")
    @SysLog("新增课程")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('course_add')")
    public R<String> create(@Valid @RequestBody EduCourseCreateDTO dto) {
        return R.ok(eduCourseService.create(dto));
    }

    /**
     * 修改课程
     *
     * @param dto 课程
     * @return R
     */
    @Operation(summary = "修改课程", description = "修改课程")
    @SysLog("修改课程")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('course_edit')")
    public R<Boolean> update(@Valid @RequestBody EduCourseUpdateDTO dto) {
        return R.ok(eduCourseService.update(dto));
    }

    /**
     * 发布课程
     *
     * @param courseCode 课程编号
     * @return R
     */
    @Operation(summary = "发布课程", description = "发布课程")
    @SysLog("发布课程")
    @PutMapping("/push/{courseCode}")
    @PreAuthorize("@pms.hasPermission('course_push')")
    public R<Boolean> pushAll(@Schema(description = "课程编号", example = "1") @PathVariable("courseCode") String courseCode) {
        return R.ok(eduCourseService.pushAll(courseCode));
    }

    /**
     * 给选中的学生发布课程
     *
     * @param dto 课程编号
     * @return R
     */
    @Operation(summary = "给选中的学生发布课程", description = "给选中的学生发布课程")
    @SysLog("给选中的学生发布课程")
    @PutMapping("/push")
    @PreAuthorize("@pms.hasPermission('course_push')")
    public R<Boolean> push(@Valid @RequestBody EduCourseResourcePushDTO dto) {
        return R.ok(eduCourseService.push(dto));
    }

    /**
     * 通过id删除课程
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除课程", description = "通过id删除课程")
    @SysLog("通过id删除课程")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('course_del')")
    public R<Boolean> removeById(@RequestBody Long[] ids) {
        return R.ok(eduCourseService.removeBatchByIds(CollUtil.toList(ids)));
    }

}